package com.controller;

import com.alibaba.fastjson.JSON;
import com.doMain.TableVO;
import com.pojo.SysUser;
import com.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/toList")
    public String in(){
        return "userList";
    }
//  列表
    @GetMapping(value = "list",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(String realName,Integer roleId,Integer page,Integer limit){
        List<SysUser> sysUser=sysUserService.list(realName,roleId,page,limit);
        int count=sysUserService.count(realName,roleId);
        TableVO tableVO = new TableVO(0,count,sysUser,"");
        String json=JSON.toJSONString(tableVO);
        return json;
    }

//    视图
    @GetMapping(value = "/view/{userId}",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String view(@PathVariable("userId") Integer id){
        SysUser sysUser = sysUserService.selectId(id);
        String json=  JSON.toJSONString(sysUser);
        return json;
    }
    @GetMapping(value = "/view2/{userId}")
    public String view2(Model model,@PathVariable("userId") Integer id){
        SysUser sysUser = sysUserService.selectId(id);
        model.addAttribute("sysUser",sysUser);
        return "userView";
    }

//    删除
    @GetMapping("/delete/{id}")
    @ResponseBody
    public String delete(Model model, @PathVariable Integer id){
        int result=sysUserService.del(id);
        return result>0?"1":"0";
}

//  To添加
    @GetMapping("/toUserAdd")
    public String toUserAdd(){
        return "userAdd";
    }

    //添加
    @PostMapping("/userAdd")
//    @ResponseBody
//    @Valid 此注解的意思是校验信息,非空日期验证等
     public String add(Model model,SysUser sysUser){
        int add=sysUserService.add(sysUser);
//        return add>0? "1":"0";
        if (add>0) {
            return "redirect:toList";
        }
        model.addAttribute("errorMsg","新增失败");
        return "userAdd";
    }

    //判断用户是否存在
    @GetMapping("/accountExists")
    @ResponseBody  //需要返回数据时使用此注解
    public String exists(String account){
        int result=sysUserService.selectAccount(account);
        return result>0? "1":"0";
    }

    @GetMapping("/toEdit/{id}")
    public String toEdit(Model model,@PathVariable Integer id){
        SysUser sysUser=sysUserService.selectId(id);
        model.addAttribute("sysUser",sysUser);
        return "userEdit";
    }

    @GetMapping("/edit")
    @ResponseBody
    public void edit(HttpServletResponse response, SysUser sysUser) throws IOException {
        int result=sysUserService.edit(sysUser);
        if (result>0) {
            response.getWriter().print("1");
        }
        else {
            response.getWriter().print("0");
        }
    }


    @GetMapping("index")
    public String index(){
        return "index_v1";
    }
    @GetMapping("menu")
    public String menu(){
        return "userLists";
    }
}
